package com.example.zjx8969.myapplication;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.bitmap.TransformationUtils;
import com.example.zjx8969.myapplication.view.CircleAvatarView;
import com.examples.DialogManagerKt;

import java.security.MessageDigest;

public class Main2Activity extends AppCompatActivity {
    private static final String avatarUrl = "https://profile.csdnimg.cn/F/A/2/3_love__coder";
    Button button;
    CircleAvatarView circleImageView;
  /*  ImageView imageView;
    private ValueAnimator animator;
    private RadarAnimationView radarAnimationView;*/

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_constraint);
//        imageView = findViewById(R.id.image);
        button = findViewById(R.id.btn_a);
        /*circleImageView = findViewById(R.id.circleImageView);*/

//        radarAnimationView = findViewById(R.id.circleImageView2);

    }


    public void onHeightClicked(View view) {
//        radarAnimationView.reverseStatus();
        DialogManagerKt.INSTANCE.add();
        circleImageView.setBitmap(avatarUrl);
        int diameter = (int) (getResources().getDisplayMetrics().density * 38);
/*        Glide.with(this).asBitmap().load(avatarUrl).transform(new CircleCorpWithBackgroundTransform(getResources(), diameter, R.drawable.explore_artist_avatar_bg))
                .into(imageView);*/

/*        Glide.with(this).asBitmap().load(avatarUrl)*//*.diskCacheStrategy(DiskCacheStrategy.NONE)*//*
                .transform(new RoundRectTransform((int) (getResources().getDisplayMetrics().density * 10)))
                .into(imageView);*/

/*        ValueAnimator animator = ValueAnimator.ofInt(button.getHeight(), getResources().getDimensionPixelOffset(R.dimen.edit_colorpicker_layout_height));
        animator.setDuration(200);

        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                ViewGroup.LayoutParams layoutParams = button.getLayoutParams();
                layoutParams.height = (int) animation.getAnimatedValue();
                button.setLayoutParams(layoutParams);
            }
        });
        animator.start();*/

    }

    private static class CircleCorpWithBackgroundTransform extends BitmapTransformation {
        private Resources resources;
        private int insideDiameter;
        private int outsideResourcesId;//R.drawable.explore_artist_avatar_bg

        public CircleCorpWithBackgroundTransform(Resources resources, int insideDiameter, int outsideResourcesId) {
            this.insideDiameter = insideDiameter;
            this.resources = resources;
            this.outsideResourcesId = outsideResourcesId;
        }

        @Override
        protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
            Bitmap bitmap = TransformationUtils.circleCrop(pool, toTransform, outWidth, outHeight);
            Bitmap backgroundBitmap = BitmapFactory.decodeResource(resources, outsideResourcesId);
            Bitmap result = Bitmap.createBitmap(backgroundBitmap.getWidth(), backgroundBitmap.getHeight(), backgroundBitmap.getConfig());
            Canvas canvas = new Canvas(result);
            Paint paint = new Paint();
            paint.setAntiAlias(true);

            canvas.drawBitmap(backgroundBitmap, 0, 0, paint);

            int ml = (backgroundBitmap.getWidth() - insideDiameter) >> 1;
            int mt = (backgroundBitmap.getHeight() - insideDiameter) >> 1;
            Rect rect = new Rect(ml, mt, backgroundBitmap.getWidth() - ml, backgroundBitmap.getHeight() - mt);

            canvas.drawBitmap(bitmap, null, rect, paint);
            return result;
        }

        @Override
        public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
            messageDigest.update((insideDiameter + "@CircleCorpWithBackgroundTransforms").getBytes());
        }
    }
}
